package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzyl.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.domain.dto.NursingPlanDto;
import com.zzyl.nursing.domain.vo.NursingPlanVo;
import com.zzyl.nursing.domain.vo.NursingProjectPlanVo;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.service.INursingProjectPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author sakula
 * @date 2025-09-12
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlanVo nVo = new NursingPlanVo();
        //1.根据id查询计划
        NursingPlan nursingPlan = getById(id);
        BeanUtils.copyProperties(nursingPlan, nVo);
        nVo.setId(nursingPlan.getId().toString());
        //2.查询计划项目
        List<NursingProjectPlanVo> plans = nursingProjectPlanMapper.getByPlanId(id);
        nVo.setProjectPlans(plans);
        return nVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlan) {
        //1.保存计划
        NursingPlan np = new NursingPlan();
        BeanUtils.copyProperties(nursingPlan, np);
        this.save(np);
        //2.保存计划项目
        List<NursingProjectPlan> plans = nursingPlan.getProjectPlans();
        if (plans != null && !plans.isEmpty()) {
            //批量设置计划id
            plans.forEach(plan -> plan.setPlanId(np.getId()));
            //批量保存
            nursingProjectPlanService.saveBatch(plans);
        }
        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlan) {
        //1.修改计划
        NursingPlan np = new NursingPlan();
        BeanUtils.copyProperties(nursingPlan, np);
        updateById(np);
        //2.删除计划项目 delete nursing_project_plan where plan_id = ?
        LambdaUpdateWrapper<NursingProjectPlan> eq = new LambdaUpdateWrapper<NursingProjectPlan>()
                .eq(NursingProjectPlan::getPlanId, np.getId());
        nursingProjectPlanService.remove(eq);
        //3.新增计划项目
        List<NursingProjectPlan> plans = nursingPlan.getProjectPlans();
        if (plans != null && !plans.isEmpty()) {
            //批量设置计划id
            plans.forEach(plan -> plan.setPlanId(np.getId()));
            nursingProjectPlanService.saveBatch(plans);
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return removeBatchByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlanVo> getAll() {

        return nursingPlanMapper.getAll();
    }
}
